package com.ffapp.rainmonitorcustom.alarm;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisApp;
import com.ffapp.rainmonitorcustom.bean.BaseListData;
import com.ffapp.rainmonitorcustom.bean.BaseListDataListBean;
import com.flyco.dialog.widget.NormalDialog;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PopHandle extends NormalDialog {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    Activity mActivity;
    private BaseListDataListBean mListBean;
    private MyAdapterList mAdapter;

    public PopHandle(Activity context) {
        super(context);
//            setCanceledOnTouchOutside(false);
        mActivity = context;
    }

    @Override
    public View onCreateView() {
//        return super.onCreateView();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.alarm_pop_handle, null);
        ButterKnife.bind(this, inflate);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext,2);
        recyclerview.setLayoutManager(layoutManager);

//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SystemParamsUtils.getScreenWidth(), LinearLayout.LayoutParams.MATCH_PARENT);
//        recyclerview.setLayoutParams(params);


        return inflate;
    }

    //    @Override
    public View onCreatePopupView() {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.alarm_pop_handle, null);
        ButterKnife.bind(this, inflate);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext,2);
        recyclerview.setLayoutManager(layoutManager);

//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SystemParamsUtils.getScreenWidth(), LinearLayout.LayoutParams.MATCH_PARENT);
//        recyclerview.setLayoutParams(params);


        return inflate;
    }

    @Override
    public void setUiBeforShow() {
            initData();
    }

    private void initData() {
        mListBean = new BaseListDataListBean();
        BaseListData data;
        data = new BaseListData("","误报");
        mListBean.getList().add(data);
        data = new BaseListData("","故障");
        mListBean.getList().add(data);
        data = new BaseListData("","火警");
        mListBean.getList().add(data);
        data = new BaseListData("","屏蔽告警");
        mListBean.getList().add(data);
        data = new BaseListData("","异常处理");
        mListBean.getList().add(data);
        mAdapter = new MyAdapterList();
        recyclerview.setAdapter(mAdapter);
    }


    @OnClick(R.id.img_close)
    void close(){
        dismiss();
    }
    @OnClick(R.id.btn_ok)
    void post(){
        BasisApp.showToast("操作成功");
        dismiss();
    }

    public class MyAdapterList extends XRecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.alarm_handle_grid_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            BaseListData data = mListBean.getList().get(position);
            viewHolder.text_name.setText(data.getName());
        }


        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
            }


        }
    }
}